package control;

import entities.Folder;
import java.io.IOException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import util.ServletUtil;

@WebServlet(name="DeleteFolder", urlPatterns={"/DeleteFolder"})
public class DeleteFolder extends HttpServlet {
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        EntityManagerFactory fact = Persistence.createEntityManagerFactory("vh11_WebApplication1PU");
        EntityManager em = fact.createEntityManager();
        
        try {
            long folderId = ServletUtil.getLong(request.getParameter("folder_id"), -1);
            Folder folder = em.find(Folder.class, folderId);

            if (folder != null) {
                em.getTransaction().begin();
                em.remove(folder);
                em.getTransaction().commit();
            }
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        finally {
            em.close();
            fact.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
